package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.FreshComment;

public class FreshCommentRowMapper implements RowMapper<FreshComment> {

    @SuppressWarnings("unused")
    private int type;

    public FreshCommentRowMapper() {
    }

    public FreshCommentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public FreshComment mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new FreshComment(), rs);
    }

    public static FreshComment parseHome(FreshComment h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static FreshComment parseHome(FreshComment h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setFcid(rs.getLong(su+"fcid"));
        } catch (SQLException e) {
            h.setFcid(null);
        }
        try {
            h.setFcuid(rs.getLong(su+"fcuid"));
        } catch (SQLException e) {
            h.setFcuid(null);
        }
        try {
            h.setRfcuid(rs.getLong(su+"rfcuid"));
        } catch (SQLException e) {
            h.setRfcuid(null);
        }
        try {
            h.setFid(rs.getLong(su+"fid"));
        } catch (SQLException e) {
            h.setFid(null);
        }
        try {
            String str = rs.getString(su+"content");
            if(str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            String str = rs.getString(su+"rcontent");
            if(str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            
            h.setRcontent(str);
        } catch (SQLException e) {
            h.setRcontent(null);
        }
        try {
            h.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            h.setType(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setNickname(rs.getString(su+"nickname"));
        } catch (SQLException e) {
            h.setNickname(null);
        }
        try {
            h.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            h.setHeadimg(null);
        }
        try {
            h.setRnickname(rs.getString(su+"rnickname"));
        } catch (SQLException e) {
            h.setRnickname(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, FreshComment h) {
        par.put("fcid",h.getFcid());
        par.put("fcuid",h.getFcuid());
        par.put("rfcuid",h.getRfcuid());
        par.put("fid",h.getFid());
        par.put("content",h.getContent());
        par.put("rcontent",h.getRcontent());
        par.put("type",h.getType());
        par.put("cdate",h.getCdate());
        par.put("nickname",h.getNickname());
        par.put("headimg",h.getHeadimg());
        par.put("rnickname",h.getRnickname());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, FreshComment h) {
        args.put("fcid",h.getFcid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final FreshComment h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "fcid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(FreshComment h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into fresh_comment(");
        int count = 0;
        if (null != h.getFcuid()) {
            sql.append("fcuid,");
            count++;
        }
        if (null != h.getRfcuid()) {
            sql.append("rfcuid,");
            count++;
        }
        if (null != h.getFid()) {
            sql.append("fid,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getRcontent()) {
            sql.append("rcontent,");
            count++;
        }
        if (null != h.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, FreshComment h)
        throws SQLException {
        int count = 1;
        if (null != h.getFcuid()) {
            ps.setLong(count++,h.getFcuid());
        }
        if (null != h.getRfcuid()) {
            ps.setLong(count++,h.getRfcuid());
        }
        if (null != h.getFid()) {
            ps.setLong(count++,h.getFid());
        }
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent() );
        }
        if (null != h.getRcontent()) {
            ps.setString(count++,h.getRcontent() );
        }
        if (null != h.getType()) {
            ps.setShort(count++,h.getType() );
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
